<template>
  <div
    ref="main"
    :class="className"
    :style="{ height: height, width: width }"
  ></div>
</template>

<script>
import echarts from "echarts";
//引入色彩主题文件
import beeInspired from "echarts/theme/bee-inspired";
// require("echarts/theme/fruit");
import resize from "./mixins/resize";
export default {
  mixins: [resize],

  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "500px",
    },
    legendData: {
      type: Array,
      default: () => ["前端", "java", "移动端", "大数据"],
    },
    seriesData: {
      type: Array,
      default: () => [
        { value: 100, name: "前端" },
        { value: 200, name: "java" },
        { value: 300, name: "移动端" },
        { value: 400, name: "大数据" },
      ],
    },
  },
  data() {
    return {
      chart: null,
    };
  },

  components: {},
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      //第二个参数是引用的主题
      this.chart = echarts.init(this.$refs.main, "beeInspired");
      var option = {
        title: {
          text: "各技术文章统计",
          subtext: "纯属虚构",
          left: "center",
        },
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/>{b} : {c} ({d}%)",
        },
        legend: {
          orient: "vertical",
          left: "left",
          data: this.legendData,
        },
        series: [
          {
            name: "统计内容",
            type: "pie",
            radius: "55%",
            center: ["50%", "60%"],
            data: this.seriesData,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };
      this.chart.setOption(option);
    },
  },
};
</script>

<style scoped>
</style>